/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.*;
import entities.*;
import java.util.ArrayList;

/**
 *
 * @author lion
 */
public class FeedbackDao {

    private Connection conn = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;
    private Statement stm = null;
    MysqlHelper mysql = new MysqlHelper();

    public void sendFeedback(Feedback feedback) throws Exception {
        try {
            conn = mysql.GetConnection();
            ps = conn.prepareStatement(
                    "INSERT INTO fl_feedback VALUES(DEFAULT,?,?,?)");
            ps.setString(1, feedback.getFeedbackText());
            ps.setString(2, feedback.getDateOfPosting());
            ps.setInt(3, feedback.getStudentid());
            ps.executeUpdate();
        } catch (Exception e) {
            throw e;
        }
    }

    public ArrayList<Feedback> getFeedbackList() throws Exception {
        ArrayList<Feedback> feedbackList = new ArrayList<Feedback>();
        try {
            conn = mysql.GetConnection();
            rs = mysql.ExecuteReader("SELECT * FROM fl_feedback");
            while (rs.next()) {                
                Feedback feedback = new Feedback();
                feedback.setFeedbackid(rs.getInt("feedbackid"));
                feedback.setFeedbackText(rs.getString("feedbackText"));
                feedback.setDateOfPosting(rs.getString("dateOfPosting"));
                feedback.setStudentid(rs.getInt("studentid"));
                feedbackList.add(feedback);
            }
        } catch (Exception e) {
            throw e;
        }
        return feedbackList;
    }
       public ArrayList<Feedback> getFeedbackList(String date) throws Exception {
        ArrayList<Feedback> feedbackList = new ArrayList<Feedback>();
        try {
            conn = mysql.GetConnection();
            rs = mysql.ExecuteReader("SELECT * FROM fl_feedback WHERE dateOfPosting='" + date+ "'");
            while (rs.next()) {                
                Feedback feedback = new Feedback();
                feedback.setFeedbackid(rs.getInt("feedbackid"));
                feedback.setFeedbackText(rs.getString("feedbackText"));
                feedback.setDateOfPosting(rs.getString("dateOfPosting"));
                feedback.setStudentid(rs.getInt("studentid"));
                feedbackList.add(feedback);
            }
        } catch (Exception e) {
            throw e;
        }
        return feedbackList;
    }
}
